package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.pms.pojo.PmsProductAttribute;
import com.imti.mall.model.pms.pojo.PmsProductCategoryAttributeRelation;
import com.imti.mall.model.pms.vo.PmsProductAttributeVo;
import com.imti.mall.product.service.PmsProductAttributeService;
import com.imti.mall.product.service.PmsProductAttributeValueService;
import com.imti.mall.product.service.PmsProductCategoryAttributeRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductAttributeController
 * Description:
 * date: 2022/9/13 17:28
 *
 * @author: youhun
 * @version:
 * @since JDK 1.8
 */
@RestController
@Api(tags = "商品属性参数管理")
@RequestMapping("/api/v1/product/attribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    @Autowired
    private PmsProductAttributeValueService pmsProductAttributeValueService;

    @Autowired
    private PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;

    @ApiOperation("添加商品属性参数数据")
    @PostMapping(value = "/addPmsProductAttribute")
    public CommonResult addPmsProductAttribute(@RequestBody PmsProductAttribute pmsProductAttribute) {
        boolean req = pmsProductAttributeService.addPmsProductAttribute(pmsProductAttribute);
        if(req){
            return CommonResult.success("添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }

    }

    @ApiOperation("修改商品属性参数数据")
    @PostMapping(value = "/updatePmsProductAttribute")
    public CommonResult updatePmsProductAttribute(@RequestBody PmsProductAttributeVo pmsProductAttributeVo) {
        Long[] ids=pmsProductAttributeVo.getIds();
        boolean req =false;
        if(ids!=null){
            for (int i=0;i<ids.length;i++){
                req =false;
                pmsProductAttributeVo.setId(ids[i]);
                req = pmsProductAttributeService.updatePmsProductAttribute(pmsProductAttributeVo);
            }
        }else if(pmsProductAttributeVo.getId()!=null){
            req = pmsProductAttributeService.updatePmsProductAttribute(pmsProductAttributeVo);
        }

        if(req){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }

    @ApiOperation("删除商品属性参数数据")
    @PostMapping(value = "/deletePmsProductAttribute")
    public CommonResult deletePmsProductAttribute(PmsProductAttributeVo pmsProductAttributeVo) {
        Long[] ids=pmsProductAttributeVo.getIds();
        boolean req =false;
        PmsProductAttribute pmsProductAttribute =new PmsProductAttribute();
        for (int i=0;i<ids.length;i++){
            req =false;
            pmsProductAttribute.setId(ids[i]);
            req = pmsProductAttributeService.deletePmsProductAttribute(pmsProductAttribute);
        }

        if(req){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }

    }

    /**
     * 查询商品列表
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询商品参数列表")
    @PostMapping(value = "/findPmsProductAttribute" )
    public CommonResult findPmsProductAttribute
    ( PmsProductAttribute pmsProductAttribute,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsProductAttribute> pmsProductAttributeCategoryList = pmsProductAttributeService.findPmsProductAttribute(pmsProductAttribute,pageSize, pageNum);
        int total=pmsProductAttributeService.getPmsProductAttribute(pmsProductAttribute);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("list",CommonPage.restPage(pmsProductAttributeCategoryList));
        map.put("total",total);
        return CommonResult.success(map);
    }

    /**
     * 查询商品参数详情
     * @param Id
     * @return
     */
    @ApiOperation("根据ID获取商品参数详情")
    @GetMapping(value = "/getPmsBrandById/{Id}")
    public CommonResult<PmsProductAttribute> getPmsBrandById(@PathVariable("Id") Integer Id) {
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeService.getPmsProductAttributeById(Id);
        if(pmsProductAttribute!=null){
            return CommonResult.success(pmsProductAttribute);
        }else {
            return CommonResult.failed("未查询到信息");
        }

    }

    /**
     * 查询商品分类和属性的关系表
     * @return
     */
    @ApiOperation(value = "查询商品分类和属性的关系表")
    @GetMapping (value = "/findPmsProductCategoryAttributeRelation/{productCategoryId}" )
    public CommonResult findPmsProductCategoryAttributeRelation
    ( PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation) {
        List<PmsProductCategoryAttributeRelation> list = pmsProductCategoryAttributeRelationService.findPmsProductCategoryAttributeRelation(pmsProductCategoryAttributeRelation);
        return CommonResult.success(list);
    }
}
